<template>
	<view class="container padding">
		<view class="balance bg-theme">
			<view class="flex-row">
				<text>当前可用金币</text>
				<text class="margin-left-sm">{{ money }}</text>
			</view>
			<view class="rate">提现费率：{{ tx_rate }} （单笔提现最低：{{lowCoin}}个）</view>
			<view class="text-sm">换算比例 1金币等于1元</view>
		</view>
		<view class="main">
			<view class="title">输入金币个数</view>
			<view class="input">
				<input type="number" v-model="withdrawal_money" @input="inputWithdrawalMoney()" placeholder="请输入提现金额"/>
			</view>
			<view class="tip flex-row">
				<!-- <text v-if="showRealMoney">实际到账金币：{{ real_money }}个</text> -->
				<text v-if="!showRealMoney" style="color: red;">{{ tip }}</text>
				<text @click="withdrawalAll">全部提现</text>
			</view>
			<view class="notice">温馨提示：
				<view>1.请准确填写提现金额，因信息错误导致的资金损失，我们将不承担任务责任；</view>
				<view>2.到账时间次日24点以前，具体到账时间以实际到账为准。</view>
			</view>
			<button class="margin-bottom bg-theme" :disabled="izWithdraw"  @click="submitWithdraw">确认提现</button>
			<!-- <button class="margin-bottom bg-theme"  @tap="showModal">确认提现</button>
			<view class="cu-modal" :class="izmodal?'show':''" @tap="hideModal">
				<view class="cu-dialog margin bg-white">
					<view class="cu-bar bg-white flex justify-center margin">
						<view class="bg-white flex justify-center align-center">
							<image src="../../../static/img/icon_notice.png" mode="aspectFit" style="width: 80rpx;height: 80rpx;"></image>
						</view>
					</view>
					<view class="bg-white flex justify-center">
						<view class="flex justify-center" style="width: 70%;text-align: left;">
							<text class="text-df text-bold" style="line-height: 40rpx; color: #666;">您还没有进行实名认证,请认证后再进行此操作</text>
						</view>
					</view>
					<view class="cu-bar bg-white flex justify-center">
						<view class="flex justify-center margin bg-theme" style="width: 200rpx; padding:10rpx 0; border-radius: 10rpx;"  @tap="goCertification">
							<text class="text-white text-sm text-bold">立即认证</text>
						</view>
					</view>
				</view>
			</view> -->
		</view>
	</view>
</template>

<script>
import * as Profit_API from '@/api/member/profit.js'	
export default{
	data(){
		return {
			userInfo: uni.getStorageSync('userInfo'),
			money: uni.getStorageSync('money').success_money,
			// money:0,
			tx_rate: 0,
			real_money: 0,
			tx_min_money: 0,
			withdrawal_money: null,
			showPayPassword: false,
			isShowPayPassword: false,
			payPassword: '',
			showRealMoney: true,
			service_charge: 0,
			tip: '输入金币超过可提现金币',
			isLoaded: false,
			izWithdraw:true,
			izcan:false,
			lowCoin:1,
			active:0,
			izmodal: false,
		}
	},
	watch:{
		withdrawal_money(newVal){
			if(newVal!=""){
				if(parseInt(newVal)>0){
					if(parseInt(newVal)%1===0){
						if(parseInt(newVal)<=parseInt(this.money)){
							this.izWithdraw= false
							// console.log('this.izWithdraw3',this.izWithdraw)
						}else{
							this.izWithdraw= true
						}
					}else{
						this.izWithdraw= true
					}
				}else{
					this.izWithdraw= true
				}
			}else{
				this.izWithdraw= true
			}
		}
	},
	onLoad(e) {
		if(e.active){
			console.log(e)
			this.active = e.active
		}
	},
	methods: {
		// 全部提现
		withdrawalAll(){
			this.withdrawal_money = this.money
			this.inputWithdrawalMoney()
		},
		// 记录金额
		inputWithdrawalMoney(){
			if(!this.withdrawal_money || Number.parseInt(this.withdrawal_money) == 0){
				// this.withdrawal_money = ''
				this.izWithdraw = false
				return 
			}
			setTimeout(()=>{
				// 判断当前金额是否大于最大金额
				this.money = Number.parseFloat(this.money || 0)
				if(this.withdrawal_money == 0){
					// this.withdrawal_money = ''
					this.izWithdraw = false
					return;
				}
				this.withdrawal_money = Number.parseInt(this.withdrawal_money);
				if(this.withdrawal_money > this.money){
					this.showRealMoney = false
				}else{
					this.showRealMoney = true
					this.tip = '输入金币超过可提现金币'
				}
			}, 50);
		},
		// 计算实际到账金额
		countRealMoney(){
			// 这里需要动态计算当前提现金额对应的手续费是否小于最低手续费
			// if(parseInt(this.withdrawal_money))
			let serviceCharge = parseInt(this.withdrawal_money) * this.tx_rate * 0.01;
			if(serviceCharge < this.service_charge){
				serviceCharge = this.service_charge;
			}
			this.real_money = Number.parseInt(this.withdrawal_money - serviceCharge).toFixed(0);
		},
		goCertification(){
			this.hideModal()
			setTimeout(()=>{
				uni.navigateTo({
					url:'./certification'
				})
			},200)
		},
		showModal() {
			this.izmodal = true
		},
		hideModal() {
			this.izmodal = false
		},
		// 确认提现
		submitWithdraw(){
			if(parseInt(this.withdrawal_money)<1){
				uni.showToast({
					title:'提现金币至少为1个',
					icon:'none'
				})
				this.withdrawal_money = ''
				this.izWithdraw = false
			}else{
				let _this = this;
				let params = {
					gold : parseInt(this.withdrawal_money)
				}
				Profit_API.withdrawal(params).then(res =>{
					if(res === 'SUCCEE'){
					// if(res === 'SUCCESS'){
						this.GET_ProfitSumMoney()
						uni.showModal({
							title:'提现已成功',
						    content: '您的收益已转入微信零钱',
							showCancel:false,
						    success: (res) => {
						        setTimeout(()=>{
						        	const pages = getCurrentPages()
						        	const prevPage = pages[pages.length-2] // 上一页
						        	prevPage.active = this.active    
						        	uni.navigateBack({
						        		delta: 1
						        	})
						        },300)
						    }
						});
					}else{
						uni.showToast({
							icon:'none',
							title:res.response.data.message,
							duration:3000,
						})
					}
				})
			}
		},
		GET_ProfitSumMoney(){
			let params = {
				member_id:uni.getStorageSync('userInfo').id
			}
			Profit_API.getProfitSumMoney(params).then(res =>{
				// console.log(res)
				uni.setStorageSync('money',res)
			})
		},
		formatePrice(price){
			if(price>0){
				return Number.parseFloat(price).toFixed(2)
			}
			return 0
		}
	}
}
</script>

<style scoped>
.container{
	background: #ededed;
	padding: 20upx 30upx;
	box-sizing: border-box;
	overflow: hidden;
}

/*主容器*/
.main{
	padding: 50upx 30upx 0;
	background: #fff;
	border-radius: 10upx;
	box-sizing: border-box;
	overflow-y: auto;
}
.main .title{
	font-size: 30upx;
	font-weight: bold;
}
.main .input{
	height: 80upx;
	line-height: 80upx;
	font-size: 28upx;
	border-bottom: 2upx solid #eee;
}
.main .input input{
	width: 100%;
	height: 100%;
}
.main .input text{
	color: #666;
	font-size: 28upx;
}
.main .input.flex-row{
	margin-top: 20upx;
}
.main .input.flex-row input{
	width: calc(100% - 130upx);
}
.main .tip{
	color: ;
	font-size: 24upx;
	margin: 20upx 0 30upx;
}
.main .notice{
	color: #666;
	margin: 30upx 0;
	font-size: 26upx;
}
.main .btn{
	height: 80upx;
	line-height: 80upx;
	text-align: center;
	border-radius: 10upx;
}

/*余额*/
.balance{
	height: 210rpx;
	overflow: hidden;
	padding: 10upx 30upx 30rpx;
	border-radius: 10upx;
}
.balance view:first-child{
	height: 80upx;
	line-height: 80upx;
}
.balance text:first-child{
	font-size: 28upx;
}
.balance text:last-child{
	font-size: 48upx;
}
.balance .rate{
	font-size: 26upx;
	padding-top: 20upx;
	border-top: 2upx solid #ddd;
}
/deep/.cu-dialog {
	width: 600rpx;
	border-radius: 30rpx;
}
</style>
